//make_unit.sqf
//#define __ACV_Trace
#include "ACV_Constantes.h"
#include "ACV_ID_main_dlg.h"
private ["_param","_mk_unit","_mk_grp","_mk_obj","_mk_grp2",
"_display","_cb_side","_cb_faction","_cb_type","_cb_unit","_cb_group",
"_cb_typegrp","_cb_unitgrp","_cb_waypoint","_sl_skill","_sl_dir","_cb_empty",
"_cb_envol","_cb_objtype","_cb_obj","_txt_sys",
"_pos","_group","_txt_side","_displayname","_type_group","_classname","_arr",
"_type","_x","_faction","_start","_s","_forindex","_custom"];
disableSerialization;
_param = _this select 0;
_mk_unit = (_param == -1);
_mk_grp = (_param == -2);
_mk_obj = (_param == -4);
_mk_grp2 = false;
_display = uiNamespace getVariable "R3F_ACV_MAIN_DLG";
_cb_side = _display displayCtrl ACV_DLG_ID_Main_CbCamp;
_cb_faction = _display displayCtrl ACV_DLG_ID_Main_CbFaction;
_cb_type	= _display displayCtrl ACV_DLG_ID_Main_CbType;
_cb_unit	= _display displayCtrl ACV_DLG_ID_Main_CbUnit;
_cb_group = _display displayCtrl ACV_DLG_ID_Main_GrpCbUnit;
_cb_typegrp = _display displayCtrl ACV_DLG_ID_Main_GrpCbTypeUnit;
_cb_unitgrp = _display displayCtrl ACV_DLG_ID_Main_GrpCbUnit;
_cb_waypoint = _display displayCtrl ACV_DLG_ID_Main_CbWayPoint;
_sl_skill = _display displayCtrl ACV_DLG_ID_Main_SliderSkill;
_sl_dir = _display displayCtrl ACV_DLG_ID_Main_SliderDir;
_cb_empty = _display displayCtrl ACV_DLG_ID_Main_ChkEmpty;
_cb_envol = _display displayCtrl ACV_DLG_ID_Main_EnVol;
_cb_objtype = _display displayCtrl ACV_DLG_ID_Main_CbObjtype;
_cb_obj = _display displayCtrl ACV_DLG_ID_Main_CbObj;
_txt_sys = _display displayCtrl ACV_DLG_ID_Main_TxtSystem;
ACV_Default_Side = lbCurSel _cb_side;
ACV_Default_Faction = lbCurSel _cb_faction;
ACV_Default_Type = lbCurSel _cb_type;
ACV_Default_Unit = lbCurSel _cb_unit;
ACV_Default_TypeGrp = lbCurSel _cb_typegrp;
ACV_Default_Group = lbCurSel _cb_group;
ACV_Default_Dir = sliderPosition _sl_dir;
ACV_Default_Skill = sliderPosition _sl_skill;
ACV_Default_Empty = lbCurSel _cb_empty;
ACV_Default_EnVol = lbCurSel _cb_envol;
ACV_Default_Obj = lbCurSel _cb_obj;
if (_mk_unit && (ACV_Default_Empty == 0)) then {
	_type = _cb_unit lbdata ACV_Default_Unit;
	if (_type != "MAN") then {	
		_mk_unit = false;
		_mk_grp2 = true;
	};
};
if ( _mk_grp || _mk_grp2) then {
	[_txt_sys, localize "STR_MSG_CLICKFORGROUP"] spawn ACV_FNCT_FlashMessage;
};
if (_mk_unit) then {
	[_txt_sys, localize "STR_MSG_CLICKFORUNIT"] spawn ACV_FNCT_FlashMessage;
};
if (_mk_obj) then {
	[_txt_sys, localize "STR_MSG_CLICKFOROBJECT"] spawn ACV_FNCT_FlashMessage;
};
ACV_Pos = false;
ACV_Void = false;
ACV_Go = false;
["R3F_ACV_CreateUnit_Click", "onMapSingleClick", { 
	ACV_Pos = _pos;ACV_Void = true;ACV_Go = true;
}] call BIS_fnc_addStackedEventHandler;
waitUntil {ACV_Void};
["R3F_ACV_CreateUnit_Click", "onMapSingleClick"] call BIS_fnc_removeStackedEventHandler;

// The following code restricts the creation of allied units and vehicles withing certain zones, if they are defined.
if (ACV_Go && !isnil "ACV_OwnSideCreationAreas") then {
	if (if(typename ACV_OwnSideCreationAreas == typename []) then { count ACV_OwnSideCreationAreas > 0 } else { false }) then {
		_txt_side = "";
		if (_mk_unit || _mk_grp) then { _txt_side = ((ACV_ARRAY_SIDE select (ACV_Default_Side)) select 1); };
		if (_mk_obj) then { if ( _cb_objtype isKindOf "AllVehicles") then { _txt_side = toUpper(str(playerSide)) }; };
		diag_log format["make_unit %1",[toUpper(_txt_side),toUpper(str(playerSide)),ACV_OwnSideCreationAreas]];
		if (toUpper(_txt_side) == toUpper(str(playerSide))) then {
			{
				if (!([ACV_Pos, _x] call R3F_FNCT_PointInShape)) exitWith {
					ACV_Go = false;
					hint localize "STR_ACV_OUTSIDEBOUNDS";
					call ACV_FNCT_CancelMakeUnit;
				};
			} foreach ACV_OwnSideCreationAreas;
		};
	};
};

if (! ACV_Go) exitWith {};
_pos = ACV_Pos;
_group = grpNull;
if (_mk_grp) then { // Création des groupes constitués
	_txt_side = ((ACV_ARRAY_SIDE select (ACV_Default_Side)) select 1);
	_displayname = _cb_group lbtext ACV_Default_Group;
	_type_group = _cb_typegrp lbdata ACV_Default_typeGrp;
	_classname = _cb_group lbdata ACV_Default_Group;
	_custom = _cb_group lbvalue ACV_Default_Group;
	_arr = [];
	_arr set [0, _txt_side];
	_arr set [1, ACV_Sel_Faction];
	_arr set [2, _type_group];
	_arr set [3, _classname];
	_arr set [4, _custom];
	ACV_NET_MakeGroups = [];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_TYPE, ACV_NET_MAKEGROUPS];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_CLASS, _classname];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_POS, _pos];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_DIR, (round ACV_Default_Dir)];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_DISPLAYNAME, _displayname];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_OWNER, (name player)];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_SIDE, _txt_side];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_SKILL, ACV_Default_Skill / 10];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_RANK, "corporal"];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_FLYING, ACV_Default_EnVol];
	ACV_NET_MakeGroups set [ACV_NET_PARAM_OTHER, _arr];
//	diag_log format["%1", ACV_NET_MakeGroups];
	if (!IS_Server) then {
		publicVariable "ACV_NET_MakeGroups";
	} else {
		[ACV_NET_MakeGroups] call ACV_FNCT_MakeGroups;
	};
};
if (_mk_grp2) then { // creation des vehicles non-vide
	_txt_side = ((ACV_ARRAY_SIDE select (ACV_Default_Side)) select 1);
	_displayname = _cb_unit lbtext ACV_Default_Unit;
	_type = _cb_unit lbdata ACV_Default_Unit;
	call compile format["_arr = ACV_ARRAY_CLASS_%1", _type];
	_classname = "";
	// diag_log Format["SelFac %1", ACV_Sel_Faction];
	_forindex = 0;
	{	if (_forindex > 0) then {
			// diag_log Format[" - %1", _x];
			if (ACV_Sel_Faction != "BIS_BAF_MTP") then {
				if (((_x select 4) == _displayname) && (ACV_Sel_Faction == getText (configFile >> "CfgVehicles" >> (_x select 3) >> "faction"))) exitwith {_classname = _x select 3;};
			} else {
				if (((_x select 4) == _displayname)) exitwith {_classname = _x select 3;};
			};
		};
		_forindex = _forindex + 1;
	} foreach _arr;
	ACV_NET_MakeVehicles = [];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_TYPE, ACV_NET_MAKEVEHICLES];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_CLASS, _classname];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_POS, _pos];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_DIR, (round ACV_Default_Dir)];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_DISPLAYNAME, _displayname];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_OWNER, (name player)];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_SIDE, _txt_side];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_SKILL, ACV_Default_Skill / 10];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_RANK, "corporal"];
	ACV_NET_MakeVehicles set [ACV_NET_PARAM_FLYING, ACV_Default_EnVol];
//	diag_log format["ACV_NET_MakeVehicles %1",ACV_NET_MakeVehicles];
	if (!IS_Server) then {
		publicVariable "ACV_NET_MakeVehicles";
	} else {
		[ACV_NET_MakeVehicles] call ACV_FNCT_MakeVehicles;
	};
};
if (_mk_unit) then { // Creation unités simples + unités véhicle vides
	_id_side = ((ACV_ARRAY_SIDE select (ACV_Default_Side)) select 0);
	_txt_side = ((ACV_ARRAY_SIDE select (ACV_Default_Side)) select 1);
	_displayname = _cb_unit lbtext ACV_Default_Unit;
	_type = _cb_unit lbdata ACV_Default_Unit;
	_faction = _cb_faction lbdata ACV_Default_Faction;
	call compile format["_arr = ACV_ARRAY_CLASS_%1",_type];
	_classname = "";
	if (count (_arr select 0) > 3) then {
		_start = -1;
	} else {
		_start = 0;
	};
	__TRACE_3("Make Unit",_txt_side,_faction,_type)
	_forindex = 0;
	{	if (_forindex > _start) then {
			__TRACE_1("Check Unit",_x)
			if (_id_side == (_x select 0)) then {
				if (_faction == (_x select 1)) then {
					if ((_x select 4) == _displayname) exitwith {_classname = _x select 3;};
				};
			};
		};
		_forindex = _forindex + 1;
	} foreach _arr;
	if (_type == "MAN") then {
		ACV_NET_MakeUnits = [];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_TYPE, ACV_NET_MAKEUNITS];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_CLASS, _classname];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_POS, _pos];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_DIR, (round ACV_Default_Dir)];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_DISPLAYNAME, _displayname];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_OWNER, (name player)];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_SIDE, _txt_side];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_SKILL, ACV_Default_Skill / 10];
		ACV_NET_MakeUnits set [ACV_NET_PARAM_RANK, "corporal"];
//		diag_log format["%1", ACV_NET_MakeUnits];
		if (!IS_Server) then {
			publicVariable "ACV_NET_MakeUnits";
		} else {
			[ACV_NET_MakeUnits] call ACV_FNCT_MakeUnits;
		};
	} else {
		if (ACV_Default_Empty == 1) then {
			ACV_NET_MakeObjects = [];
			ACV_NET_MakeObjects set [ACV_NET_PARAM_TYPE, ACV_NET_MAKESIMPLEOBJECT];
			ACV_NET_MakeObjects set [ACV_NET_PARAM_CLASS, _classname];
			ACV_NET_MakeObjects set [ACV_NET_PARAM_POS, _pos];
			ACV_NET_MakeObjects set [ACV_NET_PARAM_DIR, (round ACV_Default_Dir)];
			ACV_NET_MakeObjects set [ACV_NET_PARAM_DISPLAYNAME, _displayname];
			ACV_NET_MakeObjects set [ACV_NET_PARAM_OWNER, (name player)];
			ACV_NET_MakeObjects set [ACV_NET_PARAM_OTHER, "other"];
//			diag_log format["%1", ACV_NET_MakeObjects];
			if (!IS_Server) then {
				publicVariable "ACV_NET_MakeObjects";
			} else {
				[ACV_NET_MakeObjects] call ACV_FNCT_MakeObjects;
			};
		};
	};
};
if (_mk_obj) then { /** Creation des objects **/
	_type_obj = _cb_objtype lbtext ACV_Default_TypeObj;
	_class = _cb_obj lbdata ACV_Default_Obj;
	_displayname = _cb_obj lbtext ACV_Default_Obj;
	_s = format [Localize "STR_MSG_CREATEDBY",_displayname];
	_txt_sys ctrlSetText _s;
	ACV_NET_MakeObjects = [];
	ACV_NET_MakeObjects set [ACV_NET_PARAM_TYPE, ACV_NET_MAKESIMPLEOBJECT];
	ACV_NET_MakeObjects set [ACV_NET_PARAM_CLASS, _class];
	ACV_NET_MakeObjects set [ACV_NET_PARAM_POS, _pos];
	ACV_NET_MakeObjects set [ACV_NET_PARAM_DIR, (round ACV_Default_Dir)];
	ACV_NET_MakeObjects set [ACV_NET_PARAM_DISPLAYNAME, _displayname];
	ACV_NET_MakeObjects set [ACV_NET_PARAM_OWNER, (name player)];
	ACV_NET_MakeObjects set [ACV_NET_PARAM_OTHER, _type_obj];
//	diag_log format["%1", ACV_NET_MakeObjects];
	if (!IS_Server) then {
		publicVariable "ACV_NET_MakeObjects";
	} else {
		[ACV_NET_MakeObjects] call ACV_FNCT_MakeObjects;
	};
};
call ACV_FNCT_CancelMakeUnit;
